package cn.iocoder.yudao.module.member.service.vehicleinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.vehicleinfo.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.vehicleinfo.VehicleInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.vehicleinfo.VehicleInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 车辆管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VehicleInfoServiceImpl implements VehicleInfoService {

    @Resource
    private VehicleInfoMapper vehicleInfoMapper;

    @Override
    public Long createVehicleInfo(VehicleInfoSaveReqVO createReqVO) {
        // 插入
        VehicleInfoDO vehicleInfo = BeanUtils.toBean(createReqVO, VehicleInfoDO.class);
        vehicleInfoMapper.insert(vehicleInfo);
        // 返回
        return vehicleInfo.getId();
    }

    @Override
    public void updateVehicleInfo(VehicleInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateVehicleInfoExists(updateReqVO.getId());
        // 更新
        VehicleInfoDO updateObj = BeanUtils.toBean(updateReqVO, VehicleInfoDO.class);
        vehicleInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteVehicleInfo(Long id) {
        // 校验存在
        validateVehicleInfoExists(id);
        // 删除
        vehicleInfoMapper.deleteById(id);
    }

    private void validateVehicleInfoExists(Long id) {
        if (vehicleInfoMapper.selectById(id) == null) {
            throw exception(VEHICLE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public VehicleInfoDO getVehicleInfo(Long id) {
        return vehicleInfoMapper.selectById(id);
    }

    @Override
    public PageResult<VehicleInfoDO> getVehicleInfoPage(VehicleInfoPageReqVO pageReqVO) {
        return vehicleInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VehicleInfoDO> getVehicleInfoList(VehicleInfoPageReqVO pageReqVO) {
        return vehicleInfoMapper.selectList(pageReqVO);
    }

}